import api from "../../utils/api"

Component({
  properties: {
    mirrorInfos: {
      type: Array,
      value: []
    },
    imgUrl: {
      type: String,
      value: ''
    },
    chooseId: {
      type: String,
      value: ''
    }
  },
  attached: function () {
    let chooseType = {}
    let cityList = []
    let provinceIdList = []
    let cityChoose = ''
    let canUseList = []

    this.data.mirrorInfos.forEach(item => {
      if (!provinceIdList.includes(item.provinceId)) {
        provinceIdList.push(item.provinceId)
        cityList.push({
          provinceId: item.provinceId,
          provinceName: item.provinceName
        })
      }
      if (this.data.chooseId * 1 === -100) {
        chooseType = {}
        cityChoose = this.data.mirrorInfos[0].provinceId
      } else if (item.id * 1 === this.data.chooseId * 1) {
        chooseType = item
        cityChoose = item.provinceId
      }
    })

    this.data.mirrorInfos.forEach(item => {
      if (item.provinceId === cityChoose) {
        canUseList.push(item)
      }
    })

    this.setData({
      // chooseType: this.data.mirrorInfos[0],
      // chooseId: this.data.mirrorInfos[0].id,
      chooseId: this.data.chooseId * 1 === -100 ? '' : this.data.chooseId,
      chooseType: chooseType,
      isFullSucreen: getApp().globalData.isFullSucreen,
      cityList: cityList,
      cityChoose: cityChoose,
      canUseList: canUseList
    }, () => {
      this.hasSubscribe()
    })
  },
  data: {
    // 这里是一些组件内部数据
    someData: {},
    chooseId: null,
    chooseType: {},
    isFullSucreen: false,
    cityList: [],
    cityChoose: '',
    canUseList: [],
    buttonType: ''
  },
  methods: {
    // 这里是一个自定义方法
    cancelDiaglog() {
      this.triggerEvent("cancelDiaglog")
    },
    moveToOrder() {
      let chooseType = this.data.chooseType
      if (chooseType.hasOwnProperty('id') && chooseType.id) {
        this.triggerEvent("moveToOrder", chooseType)
      } else {
        wx.showToast({
          title: '请选择服务套餐',
          icon: 'error'
        })
      }
    },
    stop() {
      console.log('stop')
      return
    },
    typeChoose(e) {
      let id = e.currentTarget.dataset.id
      let chooseType = {}
      this.data.mirrorInfos.forEach((item) => {
        if (item.id === id) {
          chooseType = item
        }
      })
      this.setData({
        chooseId: id,
        chooseType: chooseType,
        buttonType: ''
      }, () => {
        this.hasSubscribe()
      })
    },
    chooseCity(e) {
      let cityChoose = e.currentTarget.dataset.provinceid
      let canUseList = []
      this.data.mirrorInfos.forEach(item => {
        if (item.provinceId === cityChoose) {
          canUseList.push(item)
        }
      })
      this.setData({
        cityChoose: cityChoose,
        canUseList: canUseList,
        chooseType: {
          payPrice: '',
          areaName: ''
        },
        buttonType: '',
        chooseId: ''
      })
    },
    hasSubscribe() {
      let effectiveNum = this.data.chooseType.effectiveNum * 1
      if (effectiveNum <= 0) {
        api.hasSubscribe({
          data: {
            productMirrorId: this.data.chooseType.id
          },
          success: res => {
            if (res.code * 1 === 0) {
              if (this.data.buttonType === 'alreadyBtn') {
                wx.showToast({
                  title: '取消订阅',
                  type: 'success',
                  duration: 1500
                })
              }
              this.setData({
                buttonType: res.data.hasSubscribe ? 'alreadyBtn' : 'needBtn'
              })
            }
          }
        })
      }
    },
    subscribe() {
      api.subscribe({
        data: {
          source: 4,
          productMirrorId: this.data.chooseType.id
        },
        success: res => {
          if (res.code * 1 === 0) {
            let that = this
            wx.hideLoading()
            this.hasSubscribe()
            if (this.data.buttonType === 'needBtn') {
              this.triggerEvent('needToRead')
            }

          } else {
            wx.showToast({
              title: '订阅失败，请重新订阅',
              type: 'error',
              duration: 1500
            })
          }
        }
      })
    }
  }
})